package com.cww.study.biz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cww.study.base.SysCode;
import com.cww.study.vo.SystemSetVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @program: cww_project
 * @description:
 * @author: 陈亮
 * @create: 2022-03-10 18:49
 */
@RestController
@RequestMapping("/config")
@Api(tags = "站点配置信息")
public class ConfigBiz {


    @Value("${cww.newsType}")
    private String newsType;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @ApiOperation(value = "获取新闻种类")
    @GetMapping("/getNewsType")
    @CrossOrigin
    public List<String> getNewsType() {
        return Arrays.asList(newsType.split("(,)"));
    }

    @ApiOperation(value = "站点信息设置")
    @GetMapping("/systemSet")
    @CrossOrigin
    public void systemSet(SystemSetVO vo) {
        stringRedisTemplate.opsForValue().set(SysCode.SYSTEM_SET_KEY, JSON.toJSONString(vo));
    }

    @ApiOperation(value = "站点信息获取")
    @GetMapping("/getSystemConfig")
    @CrossOrigin
    public SystemSetVO getSystemConfig() {
        String value = stringRedisTemplate.opsForValue().get(SysCode.SYSTEM_SET_KEY);
        return JSON.parseObject(value, SystemSetVO.class);
    }
}
